package fun.stgoder.test1;

import java.util.concurrent.*;

public class TestCallable {
    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
        ExecutorService executorService = Executors.newFixedThreadPool(1);
        Future<?> f1 = executorService.submit(() -> {
            System.out.println("runnable begin");
            TestUtil.sleepSeconds(3);
            System.out.println("runnable finish");
        });


        Object o = f1.get();
        System.out.println(o);


        Future<String> f2 = executorService.submit(() -> {
            System.out.println("callable begin");
            TestUtil.sleepSeconds(3);
            System.out.println("callable finish");
            return "res";
        });

        String res = f2.get(4, TimeUnit.SECONDS);
        System.out.println(res);

        Callable<String> callable = new Callable<String>() {
            @Override
            public String call() throws Exception {
                System.out.println("callable begin");
                TestUtil.sleepSeconds(3);
                System.out.println("callable finish");
                return "res";
            }
        };

        FutureTask<String> futureTask = new FutureTask<String>(callable) {
        };

        new Thread(futureTask).start();

        String res2 = futureTask.get();
        System.out.println(res2);
    }
}
